﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chenxin.Lib.House.BLL
{
    public class BlockSetting
    {
        #region  成员方法

        //根据门店ID 删除所有片区设定
        public void DelBlockByDepartId(Guid departid)
        {
            string strSql = string.Format("delete BlockSetting where DepartId='{0}'", departid);
            Chenxin.Lib.House.DAL.BlockSetting.ExecuteSQL(strSql);
        }


        //添加片区
        public bool AddBlock(Guid depid, string strblock)
        {
            try
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo departinfo = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(depid);
                if (departinfo != null)
                {
                    string strSql = "";
                    string[] arrayblock = strblock.Split(',');
                    foreach (string s in arrayblock)
                    {
                        if(!string.IsNullOrEmpty(s))
                            strSql += string.Format("insert into BlockSetting(RecordId,CityId,CompanyId,DepartId,DepartName,Block) values(newid(),'{0}','{1}','{2}','{3}','{4}');",departinfo.CityId,departinfo.CompanyId,departinfo.DepartId,departinfo.DepartName,s);
                    }
                    Chenxin.Lib.House.DAL.BlockSetting.ExecuteSQL(strSql);
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        //根据部门ID得到片区
        public string GetBlockByDepartId(Guid Departid)
        {
            string strblock = "";

            string strSql = string.Format("DepartId='{0}'", Departid);

            List<Chenxin.Lib.House.Entity.BlockSetting> blocklist = Chenxin.Lib.House.DAL.BlockSetting.GetListArray(strSql);

            if (blocklist.Count > 0)
            {
                foreach (Chenxin.Lib.House.Entity.BlockSetting bs in blocklist)
                {
                    strblock += bs.Block + ",";
                }
                if (strblock.Length > 0)
                    strblock = strblock.Substring(0, strblock.Length - 1);
            }

            return strblock;
        }

        //根据公司ID判断是否使用片区规则
        public string CheckBlockRole(Guid companyid)
        {

            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            //获取公司参数信息
            Chenxin.Lib.House.Entity.House_BusinessSetting ds_com;
            string strKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPART_SETTINGS, companyid);
            ds_com = cs.GetObject(strKey) as Chenxin.Lib.House.Entity.House_BusinessSetting;
            if (!cs.Hit)
            {
                ds_com = Chenxin.Lib.House.DAL.House_BusinessSetting.GetModel(companyid);
                if (ds_com != null) cs.Set(strKey, ds_com);
            }
            string needArea = "";
            if (ds_com == null)
                needArea = "false";
            else
                needArea = ds_com.UseBlockRole.ToString().ToLower();
            return needArea;
        }
        #endregion
    }
}
